import { reactive, ref } from "vue";

export function useFormHook<F extends {} >(initialForm:F = {} as F) {
  /**
   * 表单
   */
  const form = reactive<F>({
    ...initialForm
  });
  /**
   * 重置表单
   */
  const resetForm = () => {
    for (const key in initialForm) {
      if (Object.prototype.hasOwnProperty.call(form, key)) {
        form[key as any] = initialForm[key]
      }
    }
  };

  /**
   * 
   */
  function getFormUrlParams(){
    const params = {} as F;
    for (const key in initialForm) {
      if (Object.prototype.hasOwnProperty.call(form, key)) {
        if(form[key as any] != null || form[key as any] != undefined){
          params[key] = form[key as any];
        }
      }
    }
    return params;
  }
  return {
    form,
    resetForm,
    getFormUrlParams
  };
}
